﻿#include "pch.h"
#include "MainWindow.xaml.h"
#if __has_include("MainWindow.g.cpp")
#include "MainWindow.g.cpp"
#endif
#include "Utility.h"
#include "../src/Logging.h"


using namespace winrt;
using namespace Microsoft::UI::Xaml;
using namespace Microsoft::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Interop;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.



namespace winrt::GMTK::implementation
{
	MainWindow::MainWindow()
    {

    }



    void MainWindow::InitializeComponent()
    {
		MainWindowT<MainWindow>::InitializeComponent();
		//SetTitleBar(TitleBar());
        // 注册 Content 的 Loaded 事件处理程序
        this->Content().as<FrameworkElement>().Loaded({ this, &MainWindow::OnLoaded });
    }

    int32_t MainWindow::MyProperty()
    {
        throw hresult_not_implemented();
    }

    void MainWindow::MyProperty(int32_t /* value */)
    {
        throw hresult_not_implemented();
    }

#if 0
    bool MainWindow::IsAcrylicSupported()
    {
        // 检查是否支持 Acrylic
        return ApiInformation::IsApiContractPresent(L"Windows.Foundation.UniversalApiContract", 7);
    }

    void MainWindow::SetCustomAcrylicBackdrop()
    {
        // 获取 Compositor
        auto interop = this->try_as<IDesktopWindowXamlSourceNative2>();
        HWND hwnd;
        interop->get_WindowHandle(&hwnd);
        auto windowNative = this->try_as<IWindowNative>();
        windowNative->get_WindowHandle(&hwnd);

        auto xamlSource = DesktopWindowXamlSource();
        auto interop2 = xamlSource.as<IDesktopWindowXamlSourceNative2>();
        interop2->AttachToWindow(hwnd);

        auto compositor = Window::Current().Compositor();

        // 创建 Acrylic 背景
        auto backdropBrush = compositor.CreateHostBackdropBrush();
        auto acrylicBrush = compositor.CreateBackdropBrush();
        acrylicBrush.SourcePolicy(CompositionBackdropBrushSourcePolicy::HostBackdrop);

        auto visual = compositor.CreateSpriteVisual();
        visual.Brush(acrylicBrush);
        visual.RelativeSizeAdjustment({ 1.0f, 1.0f });

        // 将背景应用到 XAML 根元素
        auto rootGrid = this->RootGrid().as<winrt::Microsoft::UI::Xaml::UIElement>();
        auto elementVisual = ElementCompositionPreview::GetElementVisual(rootGrid);
        elementVisual.Children().InsertAtTop(visual);
    }

#endif


    void MainWindow::OnLoaded(IInspectable const&, RoutedEventArgs const&)
    {
        LOG_DEBUG(L"On Load:" + GetRuntimeClassName());
        // 确保 NavigationView 已初始化
        if (NavView().MenuItems().Size() > 0)
        {
            // 设置第一个菜单项为选中项
            NavView().SelectedItem(NavView().MenuItems().GetAt(0));

            // 导航到第一个选项
            auto firstItem = NavView().MenuItems().GetAt(0).as<NavigationViewItem>();
            if (firstItem != nullptr)
            {
                // 根据 Tag 导航到相应的页面
                NavigateToPage(firstItem.Tag().as<hstring>());
            }
        }
    }




    #include <typeinfo>

    // ...

    void MainWindow::NavView_ItemInvoked(Windows::Foundation::IInspectable const& /*sender*/, NavigationViewItemInvokedEventArgs const& args)
    {
        if (args.IsSettingsInvoked())
        {
            ContentFrame().Navigate(xaml_typename<GMTK::SettingsPage>());
        }
        else
        {
            auto invokedItem = args.InvokedItemContainer().as<NavigationViewItem>();
            if (invokedItem)
            {
                auto tag = unbox_value<hstring>(invokedItem.Tag());
				NavigateToPage(tag);
            }
        }
    }

    void MainWindow::NavigateToPage(hstring const& tag)
    {
        static std::unordered_map<hstring, TypeName> pageMapping =
        {
            { L"HomePage", xaml_typename<GMTK::HomePage>() },
            { L"SearchExcelPage", xaml_typename<GMTK::SearchExcelPage>()},
            { L"SvnOpPage", xaml_typename<GMTK::SvnOpPage>()},
        };


        auto it = pageMapping.find(tag);

        // check page is not null
        if (it == pageMapping.end())
        {
            return;
        }

        TypeName page = it->second;
        // check if the page is already loaded
        if (typeid(ContentFrame().Content().as<Windows::UI::Xaml::Controls::Page>()) == typeid(page))
        {
            return;
        }

        ContentFrame().Navigate(page);
    }


}
